/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is the "Foxmon" extension for Firefox.
*
* The developer of the Original Code is Saravana Kumar.
*
* Portions created by the Initial Developer are 
* Copyright (C) 2006 the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*   Saravana Kumar <saravanannkl@gmail.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
* ***** END LICENSE BLOCK ***** */

function FoxmonTimer(location) {
    this.Location = location;
    this.StartTime = new Date();
    this.RecentVisitStartTime = null;
    this.TotalTime = 0;
    this.StartCount = 0;
    this.StopCount = 0;
    this.LastActivityTime = null;
    
    this.Start = function() {
        try {
		this.StartCount++;
		//FoxmonLogger.DEBUG('Start - ' + this.StartCount++);
		if(!this.RecentVisitStartTime) 
			this.RecentVisitStartTime = (new Date()).getTime();
		this.LastActivityTime = this.RecentVisitStartTime;
	}
	catch(ex) { 
		FoxmonLogger.LogError('error in stop ' + ex); 
	}
    };
    
    this.Stop = function() {
	try {
		this.StopCount++;
		//FoxmonLogger.DEBUG('Stop - ' + this.StopCount++);
	
		if(!this.RecentVisitStartTime)
			return;

		var currentTime = (new Date()).getTime();
		if(currentTime - this.LastActivityTime<300000)
			this.TotalTime += (currentTime - this.RecentVisitStartTime);

		this.RecentVisitStartTime = null;
	}
	catch(ex) { 
		FoxmonLogger.LogError('error in stop ' + ex); 
	}
    };
    
    this.ResetLastActivityTime = function (action) {
	 //FoxmonLogger.DEBUG(action + this.LastActivityTime);
	 this.LastActivityTime = (new Date()).getTime();
    };
    
    this.CalculatePageVisitTime = function() {
	try {
		this.Stop();

		if(this.TotalTime<=1000) return;

		var sql = "INSERT INTO BrowsingHistory VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13)";
		var params = [
				[FoxmonStorageHelper.inputParameterMethod.Int64, this.StartTime.getTime()], 
				[FoxmonStorageHelper.inputParameterMethod.UTF8String, this.Location.href], 
				[FoxmonStorageHelper.inputParameterMethod.UTF8String, this.Location.protocol], 
				[FoxmonStorageHelper.inputParameterMethod.UTF8String, this.Location.hostname], 
				[FoxmonStorageHelper.inputParameterMethod.UTF8String, this.Location.pathname], 
				[FoxmonStorageHelper.inputParameterMethod.UTF8String, this.Location.search], 
				[FoxmonStorageHelper.inputParameterMethod.Int32, this.StartTime.getFullYear()], 
				[FoxmonStorageHelper.inputParameterMethod.Int32, this.StartTime.getMonth() + 1], 
				[FoxmonStorageHelper.inputParameterMethod.Int32, this.StartTime.getDate()], 
				[FoxmonStorageHelper.inputParameterMethod.Int32, this.StartTime.getDay()], 
				[FoxmonStorageHelper.inputParameterMethod.Int32, this.StartTime.getHours()], 
				[FoxmonStorageHelper.inputParameterMethod.Int32, this.StartTime.getMinutes()], 
				[FoxmonStorageHelper.inputParameterMethod.Int64, this.TotalTime/1000]
			];

		FoxmonStorageHelper.execute(sql, params);
	}
	catch(ex) {
		FoxmonLogger.LogError('error in stop ' + ex); 
	}
    };
}